package tech.edwardvan.springbootdemo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.edwardvan.springbootdemo.entity.Demo;
import tech.edwardvan.springbootdemo.properties.DemoProperties;

/**
 * 测试Controller
 *
 * @author EdwardVan
 */
@Api(tags = "Demo接口")
@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @Autowired
    private Demo demo;

    @Autowired
    private DemoProperties demoProperties;

    /*
        @ApiImplicitParams:用在请求的方法上，表示一组参数说明
        @ApiImplicitParam:用在@ApiImplicitParams注解中，指定一个请求参数的各个方面
            name:参数名
            value:参数的汉字说明、解释
            required:参数是否必须传
            paramType:参数放在哪个地方
                · header --> 参数在request headers中提交,请求参数的获取:@RequestHeader
                · query --> 直接跟参数完成自动映射赋值,请求参数的获取:@RequestParam
                · path(restful接口) --> 以地址的形式提交数据,请求参数的获取:@PathVariable
            dataType:参数类型
            defaultValue:参数的默认值
     */
    @ApiOperation("knife4j示例接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginUserId", value = "登录用户id", required = true, paramType = "header", dataTypeClass = String.class),
            @ApiImplicitParam(name = "id", value = "业务id", required = true, paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataTypeClass = String.class)

    })
    @PostMapping("/knife4j/{id}")
    public Object knife4jTest(@RequestHeader("loginUserId") String loginUserId, @PathVariable("id") String id, @RequestParam("name") String name, String mobile) {
        return loginUserId + ":" + id + ":" + name + ":" + mobile + ":" + demoProperties.getTest();
    }

    @ApiOperation("参数验证示例接口")
    @PostMapping("/valid")
    public Object validTest(@Validated @RequestBody Demo demo) {
        return demo;
    }

}
